<?php

namespace Modules\Idoc\Admin;

use Duxravel\Core\Events\ManageTable;
use Duxravel\Core\UI\Node;
use Duxravel\Core\UI\Table;
use Duxravel\Core\UI\Widget;
use Duxravel\Core\UI\Widget\TreeList;
use Duxravel\Core\Util\Tree;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Route;
use Modules\System\Admin\Common;
use Modules\System\Admin\Expend;

class Application extends Common
{
    public string $model = \Duxravel\Core\Model\File::class;

    public function index()
    {
        $current = Route::getCurrentRoute()->getName();
        $id = request()->input("id");
        $api_routes = Cache::get("api_routes");
        $api_routes = json_decode($api_routes,true);
        if(!$id){
            $id = $api_routes[0]['children'][0]['children'][0]['id'];
        }
        $this->assign('menuData',$api_routes);
        $this->assign('id',$id);
        $this->assign('uri',route($current));
        return $this->systemView();
//        $node = new Node();
//        $node->div(function ($node) {
//            $node->div(
//                (
//            )->class('p-2 h-10 flex-grow');
//        });
//        dump($render);
//        $table->side(function () {
//            return (new Node())->div(function ($node) {
//                $node->div(
//                    (new TreeList(request()->get('dir_id'), 'dir_id'))
//                        ->search(true)
//                        ->url(route('admin.idoc.application.ajax'))
//                        ->label('{{item.title}} ({{item.rawData.has_type}})')
//                        ->render()
//                )->class('p-2 h-10 flex-grow');
//            })->class('h-screen flex flex-col')->render();
//        }, 'left', false, '220px');
        return app_success('ok', [
            'node' => [
                [
                    'nodeName'=>'app-form',
                    'value'=>['filter'=>['id'=>'']],
                    'child'=>[
                        'nodeName'=>'div',
                        'vSlot'=>"{value: data}",
                        'class'=>'flex h-screen',
                        'child'=>[
                            [
                                'nodeName' => 'div',
                                'style' => 'width:200px',
                                'directions' => ['right'],
                                'class' => 'border-r border-gray-200 dark:border-gray-700 flex-none bg-white dark:bg-blackgray-4 h-screen',
                                'child'=>[
                                    'nodeName'=>'div',
                                    'class'=>'h-screen flex flex-col',
                                    'child'=>(new TreeList(request()->get('id'), 'id'))
                                        ->search(true)
                                        ->url(route('admin.idoc.application.ajax'))
                                        ->render()
                                ]
                            ],[
                                'nodeName' => 'app-layout',
                                'class'=>'flex-grow w-10',
                                'child' => [
                                    'nodeName' => 'div',
                                    'child'=>'测试',
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]);
    }


    public function ajax()
    {
        $api_routes = Cache::get("api_routes");
        $api_routes = json_decode($api_routes,true);
//        $trees = Tree::arr2table($api_routes,  'id', 'pid',  'name');
//        dump($trees);
//        children
        $trees = Tree::arr2tree($api_routes,  'id', 'pid',  'children');
        $table = new Table(collect($trees));
        $table->title('目录管理');
        $table->map([
            'key' => 'name',
            'title' => 'name',
            'has_type'=>''
        ]);
//        return app_success('ok',$trees);
        return $table->renderAjax();
    }

}
